package com.example.demo.api;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class TestWebSocketAPI extends TextWebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        连接建立之后自定调用
        System.out.println("TestAPI 连接成功");
    }
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        是在websocket收到消息的时候,自动调用
        System.out.println("TestAPI 收到消息"+message.toString());
        session.sendMessage(message);
    }
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
//        super.handleTransportError(session, exception);
//        连接出现异常的时候,自动调用的
        System.out.println("TestAPI 连接出现异常");

    }
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        super.afterConnectionClosed(session, status);
//        在连接正常关闭之后,自动调用的
        System.out.println("TestAPO 连接关闭");
    }
}
